<template>
	<!-- 全真模拟-在线组卷-手动组卷【选择章节】 -->
	<view>
		<u-navbar back-icon-color="#fff" :border-bottom="false" title="选择学生" title-color="#FFFFFF">
			<template v-slot:right>
				<text class="confirm" @click="confirm">确定</text>
			</template>
		</u-navbar>
		<uni-collapse accordion="true" class="chaperCon">
		    <uni-collapse-item  :title="sub.organizeName"  v-if="sub.stuList.length>0" v-for="(sub, index) in list" :key="'sub-' + sub.organizeCode">
		      <template slot="title">
		      	<u-checkbox class="inde"  @change="onSelectAll($event, sub)" v-model="sub.checked" label-disabled :name="sub.organizeCode">
		      		<text class="text-ellipsis">{{sub.organizeName }}</text></u-checkbox>
							<my-icon name="xxzj-down" style="color: #CCC;font-size:20upx; transform: scale(.7);"></my-icon>
		      </template>
		      <view class="chaperList">
		      	<view v-for="(stu, index) in sub.stuList" :key="stu.userid" class="chapter">
		      		<u-checkbox  @change="onSelect($event, sub, index)" v-model="stu.checked" :key="stu.userid" :name="sub.organizeCode + '-' + stu.userid">
		      			<text class="text-ellipsis">{{ stu.username }}</text>
		      		</u-checkbox>
		      	</view>
		      </view>
		    </uni-collapse-item>
		</uni-collapse>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			subId: '',
	   	list: [],
			choseNum:0,
			studentChose:[],
			studentName:[],
			type:0,//0考试进入  1发布通知进入
		};
	},
	onLoad(option) {
		this.type=option.type;
		let curArr=uni.getStorageSync('examStorage').studentlist
	  const defaultList=curArr.length>0?curArr.split(','):curArr;
		return new Promise((resolve, reject) => {
			this.request
				.get('/app/teacher/notice/getStudent')
				.then(res => {
					this.list = res;
					// 已选数据回显
					if(this.type==2 && defaultList.length==0) return
					for(var i=0;i<res.length;i++){
						let defaultNum=0;
						for(var j=0;j<res[i].stuList.length;j++){
							for(var g=0;g<defaultList.length;g++){
								if(res[i].stuList[j].userid==defaultList[g]){
									++defaultNum;
									this.$set(this.list[i].stuList[j],'checked', true)
									if(this.list[i].stuList.length==defaultNum){
										this.$set(this.list[i],'checked', true)
									}
								}
							}
						}
					}
					resolve();
				})
				.catch(reject)
		});
	},
	methods: {
		confirm() {
			this.checkNum();
			if(this.choseNum==0){
				uni.showToast({
					icon: 'none',
					title: '请选择学生！'
				});
				return
			}
			//选择的学生
			this.studentChose=[]
			this.studentName=[]
			for(var i=0;i<this.list.length;i++){
				for(var j=0;j<this.list[i].stuList.length;j++){
					if(this.list[i].stuList[j].checked){
						this.studentChose.push(this.list[i].stuList[j].userid)
						if(this.type==1){
							//发布通知进入
							this.studentName.push(this.list[i].stuList[j].username)
						}
					}
				}
			}
		  const sess=uni.getStorageSync('examStorage')?uni.getStorageSync('examStorage'):{};
			sess.studentlist='';
			sess.studentlist=this.studentChose.toString();
			if(this.type==1){
				sess.studentName=this.studentName.toString();
			}
			uni.setStorageSync('examStorage', sess)
			if(this.type==0){
				//返回发布考试
				uni.navigateTo({
					url:'./examNotice'
				})
			}else{
				// 返回发布通知
				uni.redirectTo({
					url:'../user/myNotic/setNotice?status=1'
				})
			}
		},
		//确认按钮确认数量
		checkNum(){
			this.choseNum==0;
			//选择数量
			for (var i = 0; i < this.list.length; i++) {
				const sub = this.list[i];
				for (var j = 0; j < sub.stuList.length; j++) {
					if (sub.stuList[j].checked === true) {
						this.choseNum++;
					}
				}
			}
		},
		// 单个选择
		onSelect({ name: ids, value }, sub, index) {
			const [subId, userid] = ids.split('-');
			if (!value) this.$set(sub, 'checked', false);
			const unChecked = sub.stuList.filter(item => !item.checked);
			if (unChecked.length === 0) this.$set(sub, 'checked', true);
		},
		// 全选
		onSelectAll({ name: checkedSubId, value }, sub) {
			for (var j = 0; j < sub.stuList.length; j++) {
				this.$set(sub.stuList[j], 'checked', value);
			}
		},
	}
};
</script>
<style>
page {
	background-color: #f5f5f5;
}
</style>
<style scoped lang="scss">
.text-ellipsis {
	display: block;
	max-width: 600upx;
	margin-left: 28upx;
}
.chapter {
	font-size: 30upx;
	font-weight: 400;
	color: #636874;
	height: 110upx;
	line-height: 110upx;
	margin-left: 70upx;
}
.chapter + .chapter {
	border-top: 1px solid #ebebeb;
}
.chaperList{
	margin-right: 30upx;
}
/deep/.u-icon__icon{
     top:0!important
}
.u-arrow-down-icon{
	margin-right: 0!important;
}
/deep/.headactive{
	border-bottom: 1px solid #EBEBEB!important;
	.iconfont{
	  transform: rotate(180deg) scale(.7)!important
	}
}
/deep/.uni-collapse-cell__title .text-ellipsis{
		font-size: 32upx;
		color: #363940;
	}
/deep/.chapter .text-ellipsis{
	font-size: 30upx;
	color: #636874;
}
</style>
